package com.otp.webapp.action;

import org.appfuse.model.User;
import org.springframework.mail.MailException;

import com.otp.service.instance.User1Manager;
import com.otp.service.util.EncryptString;
import com.otp.service.util.RandomString;
import com.otp.webapp.util.RequestUtil;


/**
 * Managed Bean to send password hints to registered users.
 *
 * <p>
 * <a href="PasswordHint.java.html"><i>View Source</i></a>
 * </p>
 *
 * @author <a href="mailto:matt@raibledesigns.com">Matt Raible</a>
 */
public class PasswordReset extends BasePage {
	private String username;
	private String email;
	private boolean sentEmail=false;
	private User1Manager user1Manager;

	public void setUsername(String username) {
		this.username = username;
	}

	public String getUsername(){
		return username;
	}

	public void setEmail(String email){
		this.email=email;
	}

	public String getEmail(){
		return email;
	}

	public boolean isSentEmail() {
		return sentEmail;
	}

	public void setSentEmail(boolean sentEmail) {
		this.sentEmail = sentEmail;
	}

	public User1Manager getUser1Manager() {
		return user1Manager;
	}

	public void setUser1Manager(User1Manager user1Manager) {
		this.user1Manager = user1Manager;
	}

	public String cancel(){
		return "cancel";
	}

	public String execute() {

		sentEmail=false;

		User user = user1Manager.getUserByEmail(email);
		if(user==null) {
			addError("errors.user.by.email.does.not.exist", new Object[]{email});
			return "/signupPasswordReset";
		}

		String password = RandomString.genStr();
		System.out.println("PASSWORD is "+password);
		String encryptedPwd = EncryptString.encrypt(password);
		
		boolean emailProblem=false;
		
        try {
        	message.setSubject(getText("login.password.reset.email.subject"));
        	setTemplateName("resetPassword.vm");
            sendUserMessage(user, new String[]{password}, RequestUtil.getAppURL(getRequest()));
        } catch (MailException me) {
            addError(me.getMostSpecificCause().getMessage());
			emailProblem=true;
            return null;
        }

/*		StringBuffer msg = new StringBuffer();
		msg.append("Your password has been reset\n");
		msg.append("See your updated login information below\n\n");
		msg.append("Username is "+user.getUsername()+"\n");
		msg.append("Password is "+password+"\n");
		msg.append("\n\nLogin at: ").append(RequestUtil.getAppURL(getRequest()));

		message.setTo(user.getEmail());
		String subject = '[' + getText("webapp.name") + "] " + getText("password.reset.email.subject");
		message.setSubject(subject);
		message.setText(msg.toString());


		boolean emailProblem=false;
		
		try{
			mailEngine.send(message);
		}catch (MailException me){
			addError(me.getMostSpecificCause().getMessage());
			emailProblem=true;
			
		}*/

		user.setPassword(encryptedPwd);
		user.setConfirmPassword(encryptedPwd);  			
		try{
			user1Manager.updateUser(user);
		}
		catch(Exception e){
			e.printStackTrace();
			addError("password.reset.save.failure");
			return "/signupPasswordReset";
		}
		
		if(emailProblem) addMessage("password.reset.success.no.email");
		else addMessage("password.reset.success.email", new Object[]{email});
		return "/signupPasswordReset";
	}
}